package quiz;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Message {

	/* ===== STATIC MEMBERS ===== */

	/**
	 * Function sets the DataBase object that will be referenced by all messages;
	 * MUST BE CALLED BEFORE ANY MESSAGES ARE CREATED
	 * @param db - the DataBase object referenced by all messages 
	 */
	public static void setDataBase(DataBase database){
		db = database;
		db.open();
	}
	public static DataBase db;

	/* ===== STATIC MEMBERS ===== */

	public static final int REQUEST = 0;
	public static final int CHALLENGE = 1;
	public static final int CHAT = 2;
	public static final int ANNOUNCEMENT = 3;




	private String content = null;
	private int type, id;
	Account from, to;
	private Quiz challengedQuiz = null;
	public boolean read;
	Date time_sent;

	/**
	 * Constructs a new Message between two Accounts.
	 * @param from the sending Account ID
	 * @param to the receiving Account ID
	 * @param content the content of the message - this is auto generated as null if it's a challenge or a friend request
	 * @param messageType - valid values are static ints Message.REQUEST, Message.CHALLENGE, Message.CHAT
	 * @param challengedQuiz - pass in null if messageType != Message.CHALLENGE, otherwise pass the quiz
	 */
	public Message(int from, int to, int type, String content, boolean read, int challengedQuiz){
		this.from = db.getAccount(from);
		this.to = db.getAccount(to);
		this.type = type;
		this.read = read;

		this.content = content;
		if(type == CHALLENGE)  this.challengedQuiz = db.getQuiz(challengedQuiz);
		if(challengedQuiz == -1) this.challengedQuiz = null;

		time_sent = new Date();
	}
	
	public Message(Account from, Account to, int type, String content, boolean has_been_read, Quiz challengedQuiz){
		this.from = from;
		this.to = to;
		this.type = type;
		this.read = read;
		this.content = content;
		if(type == CHALLENGE)  this.challengedQuiz = challengedQuiz;
		if(challengedQuiz == null) this.challengedQuiz = null;

		time_sent = new Date();

	}

	public void setID(int num) {
		this.id = num;
	}

	public void setTimeSent(Date date) {
		time_sent = date;
	}
	
	public void setHas_Been_Read(boolean bool) {
		this.read = bool;
		db.editMessage(this);
	}

	/**
	 * Returns ID of challenged quiz. If not a challenge type, returns -1
	 * @return static constant int of challenged quiz id
	 */
	public Quiz getChallengedQuiz() {
		return challengedQuiz;
	}
	
	public boolean has_been_read() {
		return read;
	}


	/**
	 * Returns content of message.
	 * @return String - content of message
	 */
	public String getContent() {
		return content;
	}

	/**
	 * Gets the time the message was created
	 * @return a java.util.Date object representing the timestamp of creation
	 */
	public Date getTimeSent(){return time_sent;}

	/**
	 * Gets the sender of this message
	 * @return the Account id from which this message was sent
	 */
	public Account getSender(){return from;}

	/**
	 * Gets the recipient of this message
	 * @return the Account id to which this message was sent
	 */
	public Account getRecipient(){return to;}

	/**
	 * Gets the type of the message: request, challenge, or chat
	 * @return either Message.REQUEST, Message.CHALLENGE, Message.CHAT
	 */
	public int getType(){return type;}

	/**
	 * Gets the SQL-table-generated id of this message
	 * @return the integer ID of the message in the message table
	 */
	public int getID(){return id;}

	@Override
	public String toString() {
		return "Message [content=" + content + ", type=" + type + ", id=" + id
				+ ", fromID=" + from.getID() + ", toID=" + to.getID() + ", challengedQuiz="
				+ challengedQuiz + ", time_sent=" + time_sent + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + challengedQuiz.getID();
		result = prime * result + ((content == null) ? 0 : content.hashCode());
		result = prime * result + from.getID();
		result = prime * result + id;
		result = prime * result
				+ ((time_sent == null) ? 0 : time_sent.hashCode());
		result = prime * result + to.getID();
		result = prime * result + type;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Message other = (Message) obj;
		if (challengedQuiz != other.challengedQuiz)
			return false;
		if (content == null) {
			if (other.content != null)
				return false;
		} else if (!content.equals(other.content))
			return false;
		if (from.getID() != other.from.getID())
			return false;
		if (id != other.id)
			return false;
		if (time_sent == null) {
			if (other.time_sent != null)
				return false;
		} else if (!sdf.format(time_sent).equals(sdf.format(other.time_sent)))
			return false;
		if (to.getID() != other.to.getID())
			return false;
		if (type != other.type)
			return false;
		return true;
	}
}